<?php
class ModelAccountGoogleIdentityToolkit extends Model {
	function git_init(){
		$this->controller_rewrite();
		$firstname 	= $this->customer->getFirstName();
		$lastname	= $this->customer->getLastName();
		$email		= $this->customer->getEmail();
		
		$display_name = $firstname.$lastname;
		$user_stat = array();
		if ($this->customer->isLogged()) {
			$user_stat = array("signedIn"=>true, "email"=>$email, "displayName"=>$display_name,"legacy"=>false);
		}
		$api_key 	= $this->config->get('google_identity_toolkit_api_key');
		$language 	= $this->config->get('google_identity_toolkit_language');
		$config_url	= $this->config->get('config_url'); 
		
		if ($language == '__auto__'){
			$language = $this->get_agent_language();
		}
		$json_user_stat = json_encode($user_stat);
		$root_url = $config_url."/index.php?route=";
		$git_js = <<<HTML
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/jsapi"></script>
<script type="text/javascript">
function load() {
	google.load("identitytoolkit", "1", {packages: ["ac"], language:"$language", callback: callback});
}
function callback() {
	var gitConfig = {
		developerKey: "$api_key",
		companyName: "Sample Store",
		callbackUrl: "$root_url"+"account/git_callback",
		realm: "",
		userStatusUrl: "$root_url"+"account/git_flogin",
		loginUrl: "$root_url"+"account/git_login",
		signupUrl: "$root_url"+"account/google_identity_toolkit_register",
		homeUrl: "$root_url"+"account/git_account",
		logoutUrl: "$root_url"+"account/logout",
		language: "en",
		idps: ["Gmail", "Yahoo", "AOL", "Hotmail"],
		tryFederatedFirst: false,
		useCachedUserStatus: false
	};
	jQuery(function(){
		window.google.identitytoolkit.setConfig(gitConfig);
		jQuery("<div id='account-chooser'></div>").appendTo("#welcome");
		jQuery('#account-chooser, #google_identitoolkit_login').accountChooser($json_user_stat);
	});
	jQuery("#header").css({"height":"98px"});
}
</script>	
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=load"></script>
HTML;
		return $git_js;
	}

	function get_agent_language(){
		$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); 
		
		$languageList = array(
			"zh-c"=>"zh-CN",
			"zh"=>"zh-TW",
			"en"=>"en",
			"fr"=>"fr",
			"it"=>"it",
			"de"=>"de",
			"es"=>"es",
			"ja"=>"ja",
			"ko"=>"ko"
		);
		
		foreach ($languageList as $lang_key=>$language){
			if (preg_match("/$lang_key/i", $lang)){
				return $language;
			}
		}
		
		return 	$languageList["en"];
	}

	function controller_rewrite(){
		if (isset($_GET['route'])){
			$route = $_GET['route'];
			$config_url = $this->config->get("config_url");
			$redirect_hash = array(
				"account/account"=>"account/git_account",
				"account/edit"=>"account/git_edit",
				"account/password"=>"account/git_password",
				"account/login"=>"account/google_identity_toolkit_login",
				"account/forgotten"=>"account/git_forgotten",
				"account/register"=>"account/google_identity_toolkit_register",
			);
			
			if (isset($redirect_hash[$route])){
				header("Location:$config_url?route=".$redirect_hash[$route]);
			}
		}
	}
	
	function updateAccountType($email, $type){
		$this->db->query("UPDATE " . DB_PREFIX . "customer SET type = '" . $type . "' WHERE email = '" . $this->db->escape($email) . "'");
	}
	
	function is_federated_login($email){
		$db = $this->registry->get('db');
		
		$customer_query = $db->query("SELECT type FROM "
		      . DB_PREFIX ."customer WHERE LOWER(email) = '"
		      . $db->escape(strtolower($email)) . "' ");
		
		if ($customer_query->num_rows) {
			if ($customer_query->row['type'] == 1) {
				return TRUE;
			} else {
				return FALSE;
			}
		}
		
		return FALSE;
	}
	
	public function addCustomer($data) {
      	$this->db->query("INSERT INTO " . DB_PREFIX . "customer SET store_id = '" . (int)$this->config->get('config_store_id') . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', password = '" . $this->db->escape(md5($data['password'])) . "', newsletter = '" . (isset($data['newsletter']) ? (int)$data['newsletter'] : 0) . "', customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "', status = '1', date_added = NOW()");
      	
		$customer_id = $this->db->getLastId();
			
      	$this->db->query("INSERT INTO " . DB_PREFIX . "address SET customer_id = '" . (int)$customer_id . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', company = '" . $this->db->escape($data['company']) . "', address_1 = '" . $this->db->escape($data['address_1']) . "', address_2 = '" . $this->db->escape($data['address_2']) . "', city = '" . $this->db->escape($data['city']) . "', postcode = '" . $this->db->escape($data['postcode']) . "', country_id = '" . (int)$data['country_id'] . "', zone_id = '" . (int)$data['zone_id'] . "'");
		
		$address_id = $this->db->getLastId();

      	$this->db->query("UPDATE " . DB_PREFIX . "customer SET address_id = '" . (int)$address_id . "' WHERE customer_id = '" . (int)$customer_id . "'");
		
		if (!$this->config->get('config_customer_approval')) {
			$this->db->query("UPDATE " . DB_PREFIX . "customer SET approved = '1' WHERE customer_id = '" . (int)$customer_id . "'");
		}	
		
		 
	}
}
?>
